package com.hongyu.smart.village.ui.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.hongyu.smart.village.VillageApp;
import com.hongyu.smart.village.event.Event;
import com.hongyu.smart.village.manager.DialogManager;
import com.hongyu.smart.village.model.response.UserLoginResp;
import com.yinglan.keyboard.HideUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment extends Fragment {
    protected Unbinder unbinder;
    protected Bundle mBundle;
    protected ProgressDialog loadingDialog;
    public UserLoginResp resp;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = getContentView(inflater, container);
        resp= VillageApp.intance.getUser();
        unbinder = ButterKnife.bind(this, view);
        HideUtil.init(getActivity(), container);
        initView();
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    /**
     * 初始化布局
     * @param inflater
     * @param container
     * @return
     */
    protected abstract View getContentView(LayoutInflater inflater, @Nullable ViewGroup container);

    /**
     * 初始化view
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
        super.onDestroy();
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    /**
     * 获取event
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMainThreadEvent(Event event) {
        if (null != event) {
            //处理事件...
            onEventProcess(event);
        }
    }

    /**
     * 处理event事件
     *
     * @param event
     */
    protected abstract void onEventProcess(Event event);

    /**
     * 跳转activity
     *
     * @param clazz
     */
    protected void sendIntent(Class<?> clazz) {
        sendIntent(clazz, null);
    }

    /**
     * 跳转activity并传值
     *
     * @param clazz
     * @param bundle
     */
    protected void sendIntent(Class<?> clazz, Bundle bundle) {
        Intent intent = new Intent(getActivity(), clazz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 跳转activity获取返回
     *
     * @param clazz
     * @param requestCode
     */
    protected void sendIntentForResult(Class<?> clazz, int requestCode) {
        sendIntentForResult(clazz, requestCode, null);
    }

    /**
     * 跳转activity获取返回并传值
     *
     * @param clazz
     * @param requestCode
     * @param bundle
     */
    protected void sendIntentForResult(Class<?> clazz, int requestCode, Bundle bundle) {
        Intent intent = getActivity().getIntent().setClass(getActivity(),clazz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    /**
     * 获取传值
     * @return
     */
    protected Bundle getBundle(){
        Intent intent = getActivity().getIntent();
        if (mBundle == null && intent != null) {
            mBundle = intent.getExtras();
        }
        return mBundle;
    }

    /**
     * 加载对话框
     * @return
     */
    protected ProgressDialog getLoadingDialog(){
        if(loadingDialog == null){
            loadingDialog = DialogManager.getLoadingDialog(getActivity());
        }
        return loadingDialog;
    }


    /**
     * 加载对话框
     * @param message
     * @return
     */
    protected ProgressDialog getLoadingDialog(String message){
        if(loadingDialog == null){
            loadingDialog = DialogManager.getLoadingDialog(getActivity());
        }
        loadingDialog.setMessage(message);
        return loadingDialog;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK ) {

        }
    }
}
